package com.xingchi.tornado.storage.core.model.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * 分片信息
 *
 * @author xingchi
 * @date 2024/5/19
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FileChunkUploadDTO {

    /**
     * 当前分片号
     */
    @Schema(description = "当前分片号")
    private Integer chunkNumber;

    /**
     * 分片大小
     */
    @Schema(description = "分片大小")
    private Long chunkSize;

    /**
     * 当前分片大小
     */
    @Schema(description = "当前分片大小")
    private Long currentChunkSize;

    /**
     * 文件总大小
     */
    @Schema(description = "文件总大小")
    private Long totalSize;

    /**
     * 分片总数
     */
    @Schema(description = "分片总数")
    private Integer totalChunks;

    /**
     * 文件标识
     */
    @Schema(description = "文件标识")
    private String sha256;

    /**
     * 文件名
     */
    @Schema(description = "文件名")
    private String filename;

    @Schema(description = "分片文件")
    private MultipartFile file;

    private byte[] content;

    public String getFilename() {
        return StringUtils.isNotBlank(filename) ? filename : file.getOriginalFilename();
    }

    public byte[] getBytes() {
        try {
            return file.getBytes();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public byte[] getContent() {
        return content == null || content.length == 0 ? getBytes() : content;
    }

}
